<template>
  <div class="nav-menu">
    <Background-Particles
      v-if="freshParticles"
      :style="{
        width,
      }"
      class="main-particles"
    />
    <div class="logo">
      <img class="img" src="~@/assets/imgs/main/logo.svg" alt="logo" />
      <span v-if="!collapse" class="title">cms</span>
    </div>

    <el-menu
      :default-active="defaultValue"
      :collapse="collapse"
      class="el-menu-vertical"
      background-color="#0c2135"
      text-color="#b7bdc3"
      active-text-color="#0a60bd"
    >
      <template v-for="item in userMenus" :key="item.id">
        <template v-if="item.type === 1">
          <el-submenu :index="item.id + ''">
            <template #title>
              <i v-if="item.icon" :class="item.icon" />
              <span>{{ item.name }}</span>
            </template>

            <template v-for="subItem in item.children" :key="subItem.id">
              <el-menu-item
                :index="subItem.id + ''"
                @click="handleMenuSelect(subItem)"
              >
                <i v-if="subItem.icon" :class="subItem.icon" />
                <span>{{ subItem.name }}</span>
              </el-menu-item>
            </template>
          </el-submenu>
        </template>

        <template v-else-if="item.type === 2">
          <el-menu-item :index="item.id + ''" @click="handleMenuSelect(item)">
            <i v-if="item.icon" :class="item.icon" />
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue';
import { useStore } from '@/store';
import { useRouter } from 'vue-router';
import { searchMenuIdByPath } from '@/utils';
import BackgroundParticles from '@/components/BackgroundParticles/index.vue';

// vuex - typescript => pinia (这个库比vuex更好的支持ts)
export default defineComponent({
  props: {
    collapse: {
      type: Boolean,
      default: false,
    },

    width: {
      type: String,
      default: '120px',
    },
  },

  components: {
    BackgroundParticles,
  },

  setup(props) {
    const store = useStore();
    const userMenus = computed(() => store.state.login.userMenus);
    const router = useRouter();
    const patchNow = router.currentRoute.value.path;
    const freshParticles = ref(true);
    const menuIdNow =
      (searchMenuIdByPath(userMenus.value, patchNow)?.id ?? '') + '';
    // console.log(menuIdNow, );
    searchMenuIdByPath(userMenus.value, patchNow);

    const defaultValue = ref(menuIdNow);

    watch(
      () => props.width,
      () => {
        freshParticles.value = false;
        setTimeout(() => {
          freshParticles.value = true;
        }, 100);
      }
    );

    const handleMenuSelect = function (e: any) {
      router.push({ path: e.url || '404' });
    };

    return {
      userMenus,
      defaultValue,
      handleMenuSelect,
      freshParticles,
    };
  },
});
</script>

<style scoped lang="less">
.nav-menu {
  height: 100%;
  background-color: #001529;
  width: 100%;

  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;

    .img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
  }

  .el-menu {
    border-right: none;
  }

  // 目录
  .el-submenu {
    background-color: #001529 !important;
    // 二级菜单 ( 默认背景 )
    .el-menu-item {
      padding-left: 50px !important;
      background-color: #0c2135 !important;
    }
  }

  ::v-deep .el-submenu__title {
    background-color: #001529 !important;
  }

  // hover 高亮
  .el-menu-item:hover {
    color: #fff !important; // 菜单
  }

  .el-menu-item.is-active {
    color: #fff !important;
    background-color: #0a60bd !important;
  }
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  height: calc(100% - 48px);
}

.main-particles {
  pointer-events: none;
  z-index: 999;
}
</style>
